<?php
/**
 * Created by PhpStorm.
 * User: Keter
 * Date: 2016/8/9
 * Time: 10:41
 */
class GenChannelVersion extends DatabaseBase
{
    public static $tableName = 'channel_version_detail';

    public function updateNotify($data)
    {
        $result = false;
        $this->isRedisLog(false);
        try {
            $result = $this->writeConfig($data);
            if ($result) {
                $result = $this->genVersion($data);
            }
        } catch (Exception $e) {
            $this->e('消息配置更新失败');
            return false;
        }

        return $result;
    }

    public function genVersion()
    {
        $cwd = getcwd();
        $newCwd = pathFormat('version/PythonTool');
        chdir($newCwd);
        $this->initMysql();

        /**
         * 执行Python脚本
         */
        $commandFile = 'MultiChannelBuildTool.py';
        $a ="python " . $commandFile;
        @exec($a, $output, $states);

        foreach ($output as $val){
            /*
             * 获取文件路径
             */
            $file    = str_replace("\\", '/', realpath($val));
            $rootDir = str_replace("\\", '/', MAJIANG_DIR);
            $file    = str_replace($rootDir . '/', '', $file);
            /*
             * 获取渠道号
             */
            $channelID = 0;
            if (preg_match('/^.*[^\d](\d+)\.apk$/', $val, $mt)) {
                $channelID = $mt[1];
            } else {
                $this->e('apk_name_error', $val);
            }
            /*
             * 获取版本号
             */
            $fileCwd = dirname($val);
            $version = substr($fileCwd, strrpos($fileCwd, '/') + 1);
            /*
             * 插入或更新数据
             */
            $data = [
                'file'       => $file,
                'modifyTime' => time()
            ];
            $where = [
                'version'    => $version,
                'channelID'  => $channelID
            ];
            $insertData = array_merge($where, $data);

            $result = $this->mysql->findRow(self::$tableName, $where);
            if ($result) {
                $this->i('channel_version_exists', $insertData);
                $result = $this->mysql->update(self::$tableName, $data, $where);
                if (!$result) {
                    $this->e($this->mysql->error());
                }
            } else {
                $insertData['createTime'] = $insertData['modifyTime'];
                $result = $this->mysql->insert(self::$tableName, $insertData);
                if (!$result) {
                    $this->e($this->mysql->error());
                }
            }
        }

        chdir($cwd);
        return true;
    }

    /**
     * 写配置
     * @param $config
     */
    public function writeConfig($data)
    {
        $content = implode("\n", $data);

        $dir = 'version/PythonTool/info/';
        if ($error = $this->checkDir($dir)) {
            $this->e($error);
        }

        //写渠道列表
        $configFile = $dir . 'channel.txt';
        $ret = $this->writeFile($configFile, $content);

        //写空的czt.txt
        $cztFile = $dir . 'czt.txt';
        if (!is_file($cztFile)) {
            $ret = touch($cztFile);
        }

        return $ret;
    }
}